package com.learning.javaweb_servlet_jdbc;

import java.sql.*;

public class JDBCTest01 {

    static final String URL = "jdbc:mysql://localhost:3306/jdbc_demo?useSSL=false&serverTimezone=GMT&characterEncoding=UTF-8";
    static final String USER = "root";
    static final String PASSWORD = "WQ9ABCAT";
    static final String SQL = "select * from student";
    public static void main(String[] args) {


        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 加载mysql驱动
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
            // 创建Statement对象
            ps = conn.prepareStatement(SQL);
            // 执行查询
            rs =ps.executeQuery();
            // 输出查询结果
            while (((ResultSet) rs).next()) {
                System.out.println(((ResultSet) rs).getObject(1)+" "+ ((ResultSet) rs).getObject(2));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (rs != null) {
                try {
                    ((ResultSet) rs).close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}